package com.huiminpay.transaction.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePayModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName : AlipayController
 * @Author : 阿泽
 * @Date: 2021/8/13 14:26
 * @Description :
 */
@Controller
public class AlipayController {
    @GetMapping("alipay/index")
    public void index(HttpServletResponse servletResponse) throws AlipayApiException, IOException {

        String serverUrl = "https://openapi.alipaydev.com/gateway.do";
        String appId = "2021000118602433";
        String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCASa1pcrtUHgZEGyTEhfR6rq4/jg+AWf4Zg3jdnyu+Sd0tOdM+I11fCZCOVtQwgwzobGtTpYUl1HoqLFcUv+nRtV5H6J3TRuVQNEgNmJ6PZn1kCrGsBJWRZmWxQcZDo58mGnVgyNv3VLm1Q0MX1riVJdufIpNhYwEjirQDGoVlyUFWi0FD7mNa2lGkS5se2M4tylikcXZSN0B2XxuU0gxK7QsUrkWLnp2Rx5FsHKlKKdCf0b53PIMuvitbQ57v2+TBBRZMWA+br1mHNgk90DjUOBNVYUuMRN3p4N32T469wPTULg5Q3R6pPo4asasy30eD4YAh9d+L9j6QvgMOvFBFAgMBAAECggEAQZXpOwy16qZC69S4smQNfh8J5GIbWLIpbx/AzgTPF0Sp97IlZhIsbSb1qV+IsZh+I+Ex6ZyoepOrNR5gErKetmCz561r6pTHLX7dC15oScXK+zdgaCIb2S7xdHK9Ct1+wrsqISXvFv5QxvOxPGHBEjc9lZjWoiY8cdA6gY+wfT1yeZ45oT3g/gVz0DG71MC9xaghZmtb+wB2w5G1fl3CHE9x8JBWJLwV99WK8y9VOUa8PUmamkAuEzySFwW+330+tSR+J0ZJH/4iIzbHCtzgxWrGFpQoQHvssTHhASXKgj011/zpFIDWrdMwwN69oPQJjv9eSf1jfKZohHmV/1hLbQKBgQC5fT6LoqW9nNiU9pZFOsBEt6WsS3j6DHKATWLKyXgxEeninmnS8I6e1tEt6YTEPcf/z2Pz+nVux6Xt4MoETVJQqJxHLm7xcV7XPZwc4tFPH83KaozklCPsnruuFxL90EC5bKpysWgzmAB0R2hrjqfHFTCFkmhROHfn57Pj+Xi6xwKBgQCxDepbbbaQZR6kkzLg/D5aqsfLBULyuvzvInMBRwsnbCG2Cw05Kapz+/7q1fVUQN20c7AW+LAQRglK5A/7ATBba/NklcQiT+GwHQydd+53I3m6+Wf7kT74Dfo8xOx6vTLodLgRblALBeuN9IL0YiaQ669u4w1tHp0Fp6+0u75wkwKBgHe6Ler3uz+ew/WzDN/2wjAY0ilaNeZAro++Y6w9SwVxaKRMWEY92/EO+8pE1tvMfRHTjdLnsXEmESZwWDVaq21AyE7x286gvPT14ZSLm0sVXkHvcK4CKQYMl1wRrMKD76NATuzlMjtLO8/SX8Y9OHK6bK1rCUaTymG4H1dhqdn1AoGAe0xMg0UhGOdCcquLiABBUBSUM/7DA5RSJhogaqhVzStRQpvppoAskr7PZCyBvx25Bzx1SVoACiUrvXqa4lqOmWvQwKBHSPnGGkXwjgyCBXewAxRCZH9ekZHyTLV6VXNCa3003farSTHjOyUmuRdM8ikfL6JwrCfgg5qmEr+XvH8CgYBviPDoL3Ut4hSdDQccIbBaHt2seUAcxFYCoNW/5E6XYYRjD5TubummKN7vLEK3mWddQuxxRc1Wbffaitk7bfn1w4ik0AZiks/3mNnw8g5TKo7YpiXXNaeXdkb8FCvWunqdjx0PxLhKjA1SCE2AtG6+153ocSgDIt2s7dujpdsNmQ==";
        String format = "json";
        String charset = "UTF-8";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmn/QlQd4+N8yMKhZTGs+xYbuRvsqeJMu6kMejblZ71g6pfX6e1tGF6QwQShM9JCrZj0fi9n0nXzsCO79892wc8ZKX7IHwpji4pdhLsMMqsI+nXNHLKmY8OQYNgjzygQBQ5g1FwIMLRtZB5UDbkvFm7v2H6kDRVILoGGOnmQDihsRzNXBAYt0bwtfZ3N9U2WnQgNexhvhKfaiC4JLW0g5cu4YZV4VuJ2y45iTU4CXUZd/kowCD5aEthAXt7PokOYrBZ3OzJitxl9ICl4C+J/C+EIPLg7tA2ou+pX4rcBByQ+91ifbb6ckF5PFw1K4O9wVkAHjavqWiLHRYuyPiq711wIDAQAB";
        String signType = "RSA2";

        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl,appId,privateKey,format,charset,alipayPublicKey,signType);
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel alipayTradeWapPayModel = new AlipayTradeWapPayModel();

        alipayTradeWapPayModel.setSubject("IPhone13 256G");
        alipayTradeWapPayModel.setOutTradeNo("T"+System.currentTimeMillis());
        alipayTradeWapPayModel.setTotalAmount("9999");
        alipayTradeWapPayModel.setQuitUrl("-");
        alipayTradeWapPayModel.setProductCode("QUICK_WAP_PAY");

//        request.setBizContent("");
        request.setBizModel(alipayTradeWapPayModel);
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
            String body = response.getBody();
            servletResponse.setContentType("text/html;charset="+charset); //编码一致
            servletResponse.getWriter().write(body);
            servletResponse.getWriter().close();
            servletResponse.getWriter().flush();

        } else {
            System.out.println("调用失败");
            servletResponse.getWriter().write("error");
        }
        servletResponse.getWriter().close();
        servletResponse.getWriter().flush();

    }
}
